package myservlet.control;
//添加供应商信息
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class addProvider extends HttpServlet{
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
			super.init(config);
			try {//加载驱动
				Class.forName("com.mysql.jdbc.Driver");				
			}
			catch(Exception e) {}
		}
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=UTF-8");//中文需设置编码
		PrintWriter out = resp.getWriter();
		//获取提交的单车信息参数  
		String pid = req.getParameter("pid");
		String name = req.getParameter("name");
		String tel= req.getParameter("tel");
		String address = req.getParameter("address");
		if(pid=="" || name=="" || tel=="" || address=="") {
			out.println("<script language = javascript>alert('请把信息填写完整!');");
			out.println("location.href='addProvider.jsp'</script>"); 
			return;
		}
		String condition="insert into provider value(?,?,?,?)";//将新记录插入到维修记录表中
		Connection con = null;
		//连接数据库
		String url = "jdbc:mysql://127.0.0.1:3306/bike?&useSSL=false&serverTimezone=GMT%2B8";
		try {
			con = DriverManager.getConnection(url,"root","744615");
			//System.out.println("连接数据库成功");
			PreparedStatement sql=con.prepareStatement(condition);
			sql.setString(1, pid);
			sql.setString(2, name);	
			sql.setString(3, tel);
			sql.setString(4, address);	
			sql.executeUpdate();
			con.close();//关闭连接
			out.println("<script language = javascript>alert('新增成功!');");
			out.println("location.href='addProvider.jsp'</script>"); 
		}
		catch (SQLException exp){
			System.out.println(exp);
			out.println("<script language = javascript>alert('新增失败!');");
			out.println("location.href='addProvider.jsp'</script>");
		}
	}
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
}


